<template>
  <div>
    <div class="bgfff mtrbl20 ptrbl20 nopb20">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="姓名" prop="userName">
            <el-input
              v-model="queryParams.userName"
              placeholder="请输入姓名"
              clearable
              size="medium"
              style="width: 200px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="昵称" prop="nickName">
            <el-input
              v-model="queryParams.nickName"
              placeholder="请输入昵称"
              clearable
              size="medium"
              style="width: 200px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input
              v-model="queryParams.phone"
              placeholder="请输入手机号"
              clearable
              size="medium"
              style="width: 200px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        </el-col>
        </el-row>
      </div>
      <div class="app-container">  
        <el-row :gutter="20">
          <!--用户数据-->
          <el-col :span="24" :xs="24">
          <el-table v-loading="loading" :data="taxUserList">
          
          <el-table-column label="序号" prop="" width="55" align="center">
            <template slot-scope="scope">
              <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
            </template>
          </el-table-column>
          <el-table-column label="用户名称" align="left" key="userName" prop="userName"/>
          <el-table-column label="用户昵称" align="left" key="nickName" prop="nickName"/>
          <el-table-column label="手机号" align="left" key="phone" prop="phone"/>
          <el-table-column label="证件号" align="left" key="idNumber" prop="idNumber" />
          <el-table-column label="客户企业" align="left" prop="companies" :show-overflow-tooltip="true" width="400">
            <template slot-scope="scope">
              <span v-for="(item,index) in scope.row.companies" :key="index">{{item.companyName+'\n'}}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="left" key="createTime" prop="createTime"/>
          <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="small" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改客户企业</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
           
            <el-form-item label="客户企业" prop="companyIds">
              <el-select v-model="form.companyIds" multiple collapse-tags filterable placeholder="请选择" @change="$forceUpdate()" style="width: 100%;">
                <el-option
                  v-for="item in companyOptions"
                  :key="item.id"
                  :label="item.companyName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
  </div>
</template>

<script>
import { taxUserList, userUpdate } from "@/api/taxmsg/taxUser";
export default {
  name: "TaxUser",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      form: {},
      // 是否显示弹出层
      open: false,
      // 是否显示详细弹出层
      openView: false,
      // 用户表格数据
      taxUserList: null,
      companyOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        userName: undefined,
        nickName: undefined,
        phone: undefined,
      },
      // 表单校验
      rules: {
        companyIds: [
          { required: true, message: "请选择客户企业", trigger: "blur" }
        ]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      taxUserList(this.queryParams).then(response => {
          this.taxUserList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      //this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
      this.isShow = false;
    },
     // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        companyIds: undefined
      };
      this.resetForm("form");
    },
    /** 修改企业 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "修改客户企业";
      //this.form = row ;

      this.form.id = row.id;
      console.log(row.companyIds.split(',').map(Number));
      this.form.companyIds = row.companyIds.split(',').map(Number);

      this.getCompanyTreeSelect().then(response => {
        this.companyOptions = response.data;
      });
     
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          userUpdate(this.form).then(response => {
              this.msgSuccess(response.msg);
              this.open = false;
              this.getList();
            }).catch(function() {});
        }
      });
    }
  }
};
</script>
<style scoped lang="scss">
</style>